// pages/modify/modify.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tel: '',//显示手机号
    count: 60,
    mobile: '',
    code: '',
    password: '',
    repassword: '',
  },
  // 获取验证码
  getVcode: function () {
    let that = this;
    // console.log(1)
    let phone = that.data.mobile;
    console.log(phone)
    let phoneReg = /^1\d{10}$/;
    if (!phoneReg.test(phone)) {
      wx.showModal({
        title: '提示',
        content: '请填入正确的手机号',
        showCancel: false
      })
      return;
    }
    var date = new Date();
    var m = date.getMinutes().toString();//获得当前分钟数
    // console.log('提交外面的')
    // console.log(m)
    // console.log(date.getMinutes())
    //开始缓存池中没有分钟数，当前分钟数肯定不等于缓存中的分钟数，当前可以执行
    if (m != wx.getStorageSync('m')) {
      wx.setStorageSync('m', m)//把分钟数放到缓存
      app.post('/personal/send_sms', "post", { mobile: phone }).then((res) => {
        let timer = setInterval(() => {
          let count = that.data.count;
          count--;
          that.setData({ count: count });
          if (count == 0) {
            clearInterval(timer);
            that.setData({ count: 60 });
          }
        }, 1000)
      })
    } else {
      //当发生了1分钟内多次点击等事件，弹窗提示或者做别的操作。
      wx.showToast({
        title: '请不要重复点击',
        duration: 1000
      })
    }

  },

  shows(msg) {
    wx.showToast({
      title: msg,
    })
  },
  formSubmit: function (e) {
    let form = e.detail.value;
    var that = this;
    const phoneReg = /^1\d{10}$/;
    if (!phoneReg.test(form.mobile)) {
      wx.showModal({
        title: '提示',
        content: '请输入正确的手机号',
        showCancel: false
      })
      return;
    }
    if (!form.code) {
      wx.showModal({
        title: '提示',
        content: '请输入验证码',
        showCancel: false
      })
      return;
    }
    if (!form.password) {
      wx.showModal({
        title: '提示',
        content: '请输入密码',
        showCancel: false
      })
      return;
    }
    if (form.password.length < 6) {
      wx.showModal({
        title: '提示',
        content: '请输入密码不能小于6位',
        showCancel: false
      })
      return;
    }
    if (!form.repassword) {
      wx.showModal({
        title: '提示',
        content: '请确认新密码',
        showCancel: false
      })
      return;
    }
    if (form.password != form.repassword) {
      wx.showModal({
        title: '提示',
        content: '两者密码不一致请重新输入',
        showCancel: false
      })
      return;
    }
    let postData = {
      ...form
    };
    app.post("/personal/forget_password", "put", postData).then(res => {
      that.shows(res.msg)
      setTimeout(function () {
        wx.navigateBack({
          delta: -1
        });
      }, 1000);

    })
    // wx.switchTab({
    //   url: '../Account/Account'
    // })
  },
  // 获取手机号
  getPhone(e) {
    let that = this;

    that.setData({ mobile: e.detail.value })
  },
  getCode(e) {
    let that = this;
    that.setData({ code: e.detail.value })
  },
  getPwd(e) {
    let that = this;
    that.setData({ password: e.detail.value })
  },
  getPwds(e) {
    let that = this;
    that.setData({ repassword: e.detail.value })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // let that = this;
    // var tel = options.tel;
    // var phone = tel.substr(0, 3) + "****" + tel.substr(7);
    // that.setData({ tel: phone })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})